using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace DD.Controls
{
    public partial class rating : System.Web.UI.UserControl
    {
        private int uid;
        private int pid;
        bool UserHasRated;
        int userrating;
        bool isUserLoggedIn;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Construct Instance Variables
            
            Visitors.Visitor v = new DD.Visitors.Visitor();
            isUserLoggedIn = v.IsLoggedIn();

            pid = Convert.ToInt32(Request["pid"]);
            
           
            UserHasRated = false;
            userrating = DD.Ratings.RatingManager.GetUserProductRating(uid, pid);


            if (isUserLoggedIn)
            {
                if (userrating > 0)
                {
                    UserHasRated = true;
                }
                CreateProductRating();
                if (UserHasRated)
                {
                    Literal l = new Literal();
                    l.Text = "<br />";
                    PlaceHolder1.Controls.Add(l);
                    CreateUserRating();
                }

            }
            else
            {
                UserHasRated = false;
                CreateProductRating();

            }




           

        }

        private void CreateUserRating()
        {
            Literal l = new Literal();
            l.Text = "<br /><div class=\"RatingHeader\">Your Rating</div>";
            PlaceHolder1.Controls.Add(l);
            Castle.Web.Controls.Rater r = new Castle.Web.Controls.Rater();
            r.Value = userrating;
            r.MaxValue = 5;
            r.AutoPostback = true;
            r.ImageOffUrl = "/SiteImages/StarOff.gif";
            r.ImageOnUrl = "/SiteImages/StarOn.gif";
            r.ImageOverUrl = "/SiteImages/StarOver.gif";
            r.ScriptUrl = "~/Scripts/CastleRater.js";
            r.ValueChanged += new EventHandler(UR_ValueChanged);
            r.ID = "UserRater1";
            PlaceHolder1.Controls.Add(r);
        }

        private void CreateProductRating()
        {
            Literal l = new Literal();
            l.Text = "<div class=\"RatingHeader\">Product Rating</div>";
            PlaceHolder1.Controls.Add(l);
            Castle.Web.Controls.Rater r = new Castle.Web.Controls.Rater();

            if (UserHasRated)
            {
                r.Enabled = false;
            }
            if (!isUserLoggedIn)
            {
                r.Enabled = false;
            }
            r.Value = 3;
            r.MaxValue = 5;
            r.AutoPostback = true;
            r.ImageOffUrl = "/SiteImages/StarOff.gif";
            r.ImageOnUrl = "/SiteImages/StarOn.gif";
            r.ImageOverUrl = "/SiteImages/StarOver.gif";
            r.ScriptUrl = "~/Scripts/CastleRater.js";
            r.ValueChanged += new EventHandler(PR_ValueChanged);
            r.ID = "ProductRater";
            PlaceHolder1.Controls.Add(r);
        }

        void PR_ValueChanged(object sender, EventArgs e)
        {
            
           int Rating = Convert.ToInt32(((Castle.Web.Controls.Rater)FindControl("ProductRater")).Value);
           DD.Ratings.RatingManager.AddUpdateUserProductRating(uid, pid, Rating);
        }
        void UR_ValueChanged(object sender, EventArgs e)
        {

           int Rating = Convert.ToInt32(((Castle.Web.Controls.Rater)FindControl("UserRater1")).Value);
           DD.Ratings.RatingManager.AddUpdateUserProductRating(uid, pid, Rating);
        }
    }
}